define(()=>{
  P_AJAX({
    url:'../libs/goods.json',
    // type:'get'
})
.then((res)=>{
    let data=JSON.parse(res)
    data=data.slice(120,240)
    render(data)
    cart(data)
})
function render(data){
    data.forEach(item=>{
        $('.cons')[0].innerHTML+=`
        <li class="apply">
        <a href="../html/product.html?id=${item.goods_id}">
        <div class="pic">
               <img src="${item.img_small_logo}">
           </div>
           <p class="title">${item.category}</p>
           <p class="des">${item.title}</p>
           </a>
           <p class="price">${item.price}元</p>
           <span data-id="${item.goods_id}" class="shopping">+</span>
        </li>
           `
        }) 

    $(".cons").paging({
        PageNum: 15, //每页显示数目
        pageMax: true, //按钮长度是否显示
        pageMaxHideShow: false, //在最后一个的时候是否隐藏按钮长度
        pageDownUpHide: false, //到第一个或最后一个是否让上一页或下一页消失
        pageInput: true, //是否使用文本框输入跳转
        pagingBtnHide: false, //是否让按钮变为一个
        pagingBtnPaging: true,//按钮是否分页
        pagingDisplay: "flex"//显示的属性，弹性盒子还是块化
    })
}
let ul=document.querySelector('.cons')
let span=document.querySelector('.counts')
function cart(data){
  ul.addEventListener('click',(event)=>{
      let des=document.querySelector('#des')
      let target=event.target
      let id=target.dataset.id-0
      let goods=data.find(item=>item.goods_id===id)
      let list=JSON.parse(localStorage.getItem('cart')) || []
      if(target.className==='shopping'){
        if(des.innerHTML==='Hi！你好'){
            alert('亲你还没有登录哦，快去登录吧')
            goods.cart_number=0
          return false
        }
      }
      if(!list.length){
        list.push(goods)
        goods.cart_number=1
      }else{
        let res=list.some(item=>item.goods_id===id)
        if(res){
          let index=list.findIndex(item=>item.goods_id===id)
          list[index].cart_number++
        }else{
          list.push(goods)
          goods.cart_number=1
        }
      }
       

      localStorage.setItem('cart',JSON.stringify(list))
      count()
    },false)
  }
  function count(){
    let list=JSON.parse(localStorage.getItem('cart')) || []
    let count=list.reduce((some,num)=>{
      return some+num.cart_number
    },0)
    span.innerHTML=count
  }
  count()
})


